﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Xaml;
using BizControl.Library.Security;
using Csla;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using System.ComponentModel.Composition;
using BizControl.Library.BusinessObjects.Models;
using BizControl.Library.BusinessObjects.People;

namespace BizControl.Presentation.Shell.ViewModels
{
  [Export]
  public class ModelSelectorViewModel : INotifyPropertyChanged
  {
    public ModelSelectorViewModel()
    {
      this.AddModelCommand = new DelegateCommand(this.OnAddModel, this.CanAddModel);
      this.SaveModelCommand = new DelegateCommand(this.OnSaveModel, this.CanSaveModel);
    }

    public PersonBO CurrentPerson
    {
      get
      {
        return ((BizControlPrincipal)ApplicationContext.User).Person;
      }
      set
      {
        ((BizControlPrincipal)ApplicationContext.User).Person = value;
      }
    }

    public PersonModelBO CurrentModel
    {
      get
      {
        if (((BizControlPrincipal)ApplicationContext.User).Model != null)
          return CurrentPerson.Models.FirstOrDefault((m) => m.Model.Id == ((BizControlPrincipal)ApplicationContext.User).Model.ModelId);
        return null;
      }
      set
      {
        ((BizControlPrincipal)ApplicationContext.User).Model = ModelBO.GetModel(value.Model.Id);
        OnPropertyChanged("CanEnter");
      }
    }

    public bool CanEnter
    {
      get
      {
        return ((BizControlPrincipal)ApplicationContext.User).Model != null;
      }
    }

    #region Commands

    public ICommand AddModelCommand { get; private set; }

    private void OnAddModel()   
    {
      if (NewModel == null)
      {
        NewModel = ModelBO.NewModel();
      }
    }

    private bool CanAddModel()
    {
      return (NewModel == null);
    }

    public ICommand SaveModelCommand { get; private set; }

    private void OnSaveModel()
    {
      NewModel = NewModel.Save();
      CurrentPerson.Models.AddModel(NewModel);
      CurrentPerson = CurrentPerson.Save();
      NewModel = null;
    }

    private bool CanSaveModel()
    {
      if (NewModel == null) return false;
      return NewModel.IsValid;
    }

    public bool CanSaveModelProp
    {
      get { return CanSaveModel(); }
    }

    private void CommandsChanged()
    {
      ((DelegateCommand)SaveModelCommand).RaiseCanExecuteChanged();
      OnPropertyChanged("CanSaveModelProp");
      ((DelegateCommand)AddModelCommand).RaiseCanExecuteChanged();
    }

    #endregion

    ModelBO _newModel;
    public ModelBO NewModel 
    {
      get { return _newModel; }
      set 
      { 
        _newModel = value;
        OnPropertyChanged("ModelName");
        CommandsChanged();
      }
    }

    public string ModelName
    {
      get
      {
        if (NewModel == null) return string.Empty;
        return NewModel.Name;
      }
      set
      {
        if (NewModel != null) NewModel.Name = value;
        CommandsChanged();
      }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    
    protected virtual void OnPropertyChanged(string propertyName)
    {
      if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

  }
}
